{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94347b26",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import yfinance as yf\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.dates as mdates\n",
    "from matplotlib.ticker import MultipleLocator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a713415",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "def compute_drawdowns(log_returns):\n",
    "    # Calculate cumulative returns\n",
    "    # YOUR CODE HERE\n",
    "    cumulative_returns = None\n",
    "    # Calculate ongoing drawdown\n",
    "    # YOUR CODE HERE\n",
    "    rolling_max = None\n",
    "    drawdowns = None\n",
    "    return drawdowns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "144cf078",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "def plot_returns(log_returns):\n",
    "    # Create figure and axis objects\n",
    "    fig, ax = plt.subplots()\n",
    "    # Calculate cumulative returns\n",
    "    # YOUR CODE HERE\n",
    "    cumulative_returns = None\n",
    "    # Calculate ongoing drawdown\n",
    "    # YOUR CODE HERE\n",
    "    drawdowns = None\n",
    "    # Plotting the cumulative returns\n",
    "    # YOUR CODE HERE\n",
    "    # Plotting the drawdowns\n",
    "    # YOUR CODE HERE\n",
    "    # Setting x-axis major locator to each year and formatter\n",
    "    # YOUR CODE HERE\n",
    "    # Setting y-axis ticks every 20%\n",
    "    # YOUR CODE HERE\n",
    "    # Adding grid with vertical lines for each year\n",
    "    # YOUR CODE HERE\n",
    "    # Rotate x-axis labels by 45 degrees\n",
    "    # YOUR CODE HERE\n",
    "    ax.legend()\n",
    "    ax.set_title('S&P500 Cumulative Returns and Drawdowns')\n",
    "    ax.set_xlabel('Date')\n",
    "    ax.set_ylabel('Returns/Drawdown')\n",
    "    fig.tight_layout()\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69ddf292",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download S&P 500 front month futures data\n",
    "sp500_futures_prices = yf.download('ES=F')[\"Adj Close\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74696270",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Calculate logarithmic returns\n",
    "log_returns = np.log(sp500_futures_prices).diff()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3285fb4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_returns(log_returns)"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
